'use strict';

/**
 * 自定义相机事件
 */
var CustomCameraInput = React.createClass({
    displayName: 'CustomCameraInput',
    /**
     * 组件状态
     * @returns {{currentHeading: number, currentCameraPosition: null, cameraDistance: number, viewCenterCarto: Cesium.Cartographic}}
     */
    getInitialState: function getInitialState() {
        /**
         * 组件状态
         * @type {object}
         * @property {object} currentHeading 相机当前朝向
         * @property {object} currentCameraPosition 相机当前位置
         * @property {object} cameraDistance 相机视线与地表交点的距离
         * @property {object} viewCenterCarto 相机视线与地表交点的经纬度
         */
        return {
            currentHeading: 0,
            currentCameraPosition: null,
            cameraDistance: 0,
            viewCenterCarto: new Cesium.Cartographic()
        };
    },
    componentDidMount: function componentDidMount() {
        if (this.props.isMobile) return;
        var _props = this.props,
            viewer = _props.viewer,
            homeView = _props.homeView;
        //右键添加tiltEventTypes事件 实现右键旋转。注意ScreenSpaceEventType、Cesium.CameraEventType的使用与区别。
        //第一条语句把地球旋转功能赋给右键、同时禁止掉滚轮按下旋转功能。第二条语句赋值滚轮放大缩小功能的同时禁止掉右键放大缩小功能。

        viewer.scene.screenSpaceCameraController.tiltEventTypes = Cesium.CameraEventType.RIGHT_DRAG;
        viewer.scene.screenSpaceCameraController.zoomEventTypes = Cesium.CameraEventType.WHEEL;
        //ESC键盘事件。
        document.onkeydown = function (e) {
            var keycode = e.which;
            if (keycode === 27) {
                viewer.camera.flyTo(homeView);
            }
        };
    },
    componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
        //2D 3D切换
        var _props2 = this.props,
            sceneMode = _props2.sceneMode,
            viewer = _props2.viewer;

        var scene = viewer.scene;
        var camera = viewer.scene.camera;
        var windowPositionSearch = new Cesium.Cartesian2();
        if (nextProps.sceneMode !== sceneMode) {
            if (scene.mode === 3 || scene.mode === 2) {
                var windowPosition = windowPositionSearch;
                windowPosition.x = scene.canvas.clientWidth / 2;
                windowPosition.y = scene.canvas.clientHeight / 2;

                var viewCenter = new Cesium.Cartesian3();
                var carto = new Cesium.Cartographic();
                var cameraDistance = 0;
                if (scene.mode === 3) {
                    var ray = camera.getPickRay(windowPosition);
                    viewCenter = scene.globe.pick(ray, scene, new Cesium.Cartesian3());
                    carto = Cesium.Cartographic.fromCartesian(viewCenter);
                    cameraDistance = Cesium.Cartesian3.distance(camera.position, viewCenter);
                } else if (scene.mode === 2) {
                    viewCenter = camera.pickEllipsoid(windowPosition);
                    carto = Cesium.Cartographic.fromCartesian(viewCenter);
                    cameraDistance = camera.positionCartographic.height;
                }

                //两点之间的距离计算
                // console.log(camera.positionCesium.Cartographic.height);
                // let nextCameraCarto = new Cesium.Cartographic(carto.longitude,carto.latitude,cameraDistance);

                this.setState({
                    currentHeading: camera.heading,
                    viewCenterCarto: carto,
                    cameraDistance: cameraDistance
                });
            }
        }
    },
    componentDidUpdate: function componentDidUpdate(prevProps) {
        var _props3 = this.props,
            sceneMode = _props3.sceneMode,
            viewer = _props3.viewer;

        if (prevProps.sceneMode !== sceneMode) {
            var camera = viewer.scene.camera;
            var _state = this.state,
                viewCenterCarto = _state.viewCenterCarto,
                currentHeading = _state.currentHeading,
                cameraDistance = _state.cameraDistance;
            // console.log(this.state);

            if (sceneMode === 2) {
                viewer.scene.morphTo2D(0.0);
                camera.lookAt(Cesium.Ellipsoid.WGS84.cartographicToCartesian(viewCenterCarto), new Cesium.HeadingPitchRange(currentHeading, Cesium.Math.toRadians(-90), cameraDistance * 1.5));
                camera.setView({
                    orientation: {
                        heading: currentHeading
                    }
                });
                camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
            } else if (sceneMode === 3) {
                viewer.scene.morphTo3D(0.0);
                cameraDistance = cameraDistance / 1.5;
                camera.lookAt(Cesium.Ellipsoid.WGS84.cartographicToCartesian(viewCenterCarto), new Cesium.HeadingPitchRange(currentHeading, Cesium.Math.toRadians(-40), cameraDistance));
                camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
            }
        }
    },
    componentWillUnmount: function componentWillUnmount() {
        var viewer = this.props.viewer;

        viewer.scene.screenSpaceCameraController.tiltEventTypes = Cesium.CameraEventType.WHEEL;
        viewer.scene.screenSpaceCameraController.zoomEventTypes = Cesium.CameraEventType.RIGHT_DRAG;
    },
    render: function render() {
        return null;
    }
});